<?php
class Admin_Model_Product extends Zend_Db_Table_Abstract
{
    protected $_name = 'product';
    public function getAllData($a=array()){        
    	$query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p'=>$this->_name))
                        ->joinLeft(array('i'=>'productitem'),'p.itemId =i.id', array('i.name as itemName'))
                        ->joinLeft(array('c'=>'productcat'),'i.catId =c.id', array('c.name as catName'))
                        ->joinLeft(array('a'=>'area'),'p.areaId = a.id', array('a.name as areaName'))
                        ->joinLeft(array('u'=>'user'),'p.userId = u.id', array('u.username','u.email'));                           
        if(isset($a['wait'] )&& $a['wait']){            
            $query->where('p.status=?', (int)$a['wait']);
        }    
        if(isset($a['type'] )&& $a['type']){
            $query->where('p.type like ?',$a['type']);
        }   
        if(isset($a['time']) && $a['time']){
            $query->where('endDate < ?',(int)$a['time']);
        }                                                                                                    
    	return $this->fetchAll($query);
    }
    public function getAllDataOnRun($a=array()){
    	$query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p'=>$this->_name))
                        ->joinLeft(array('i'=>'productitem'),'p.itemId =i.id', array('i.name as itemName'))
                        ->joinLeft(array('c'=>'productcat'),'i.catId =c.id', array('c.name as catName'))
                        ->joinLeft(array('a'=>'area'),'p.areaId = a.id', array('a.name as areaName'))
                        ->joinLeft(array('u'=>'user'),'p.userId = u.id', array('u.username','u.email'));                           
        if(isset($a['wait'] )&& $a['wait']){
            $query->where('p.status=?', (int)$a['wait']);
        }    
        if(isset($a['type'] )&& $a['type']){
            $query->where('p.type like ?',$a['type']);
        }                      
        $query->where('startDate < ?',(int)time());
        $query->where('endDate  > ?',(int)time());                                                                                                         
    	return $this->fetchAll($query);
    }
    public function getAllDataMess(){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p'=>$this->_name))
                        ->joinLeft(array('i'=>'productitem'),'p.itemId =i.id', array('i.name as itemName'))
                        ->joinLeft(array('c'=>'productcat'),'i.catId =c.id', array('c.name as catName'))
                        ->joinLeft(array('a'=>'area'),'p.areaId = a.id', array('a.name as areaName'))
                        ->joinLeft(array('u'=>'user'),'p.userId = u.id', array('u.username','u.email'));                                   
        $query->where('p.status=?',0);        
        return $this->fetchAll($query);
    }
    public function getDataByAreaId($id){
        $query = $this->select()
                        ->from(array('p'=>$this->_name))
                        ->where('areaId = ?',(int)$id);            	
        return $this->fetchAll($query);
    }
    public function getDataById($id){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p'=>$this->_name))
                        ->joinLeft(array('i'=>'productitem'),'p.itemId =i.id', array('i.name as itemName'))
                        ->joinLeft(array('c'=>'productcat'),'i.catId =c.id', array('c.name as catName'))
                        ->joinLeft(array('a'=>'area'),'p.areaId = a.id', array('a.name as areaName'))
                        ->joinLeft(array('u'=>'user'),'p.userId = u.id', array('u.username','u.email'))
                        ->where('p.id = ?',(int)$id);                                    	
        return $this->fetchRow($query);
    }
    public function addData($data){
        $data=array('itemId'         =>$data['itemId'],
                    'userId'        =>$data['userId'],
                    'areaId'        =>$data['areaId'],
                    'title'         =>$data['title'],
                    'shortTitle'    =>$data['shortTitle'],
                    'photo'         =>$data['photo'],
                    'type'         =>$data['type'],
                    'price'         =>$data['price'],
                    'content'       =>$data['content'],
                    'contact'       =>$data['contact'],
                    'mobile'       =>$data['mobile'],
                    'emailCont'     =>$data['emailCont'],
                    'addressCont'   =>$data['addressCont'],
                    'status'        =>$data['status'],                    
                    'transaction'   =>$data['transaction'],
                    'human'         =>$data['human'],
                    'createDate'    =>$data['createDate'],
                    'startDate'     =>$data['startDate'],                                                            
                    'endDate'       =>$data['endDate'],
                    'priceAd'       =>$data['priceAd']
                    );                    
        $id=$this->insert($data, $this->_name);
        return $id;
    }
    public function updateWait($id, $data=array()){
        return $this->update($data, 'id = '.(int)$id);
    }
    public function updateData($id = '',$data){        
        $data=array('itemId'         =>$data['itemId'],
                    'userId'        =>$data['userId'],
                    'areaId'        =>$data['areaId'],
                    'title'         =>$data['title'],
                    'shortTitle'    =>$data['shortTitle'],
                    'photo'         =>$data['photo'],
                    'price'         =>$data['price'],
                    'content'       =>$data['content'],
                    'contact'       =>$data['contact'],
                    'mobile'        =>$data['mobile'],
                    'emailCont'     =>$data['emailCont'],
                    'addressCont'   =>$data['addressCont'],
                    'status'        =>$data['status'],                    
                    'transaction'   =>$data['transaction'],
                    'human'         =>$data['human'],
                    'createDate'    =>$data['createDate'],
                    'startDate'     =>$data['startDate'],                    
                    'endDate'       =>$data['endDate'],
                    'priceAd'       =>$data['priceAd'],
                    'type'          =>$data['type']);                            
        return $this->update($data, 'id = '.(int)$id);
    }
   
    public function deleteData($id){        
        $this->delete('id = '.(int)$id);
    }
}